package com.emmy.walnut.presenter;

import android.os.Handler;
import android.os.Message;

import com.emmy.walnut.app.AppDatabase;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.model.Record;
import com.emmy.walnut.presenter.iface.SingleClassDetailPIface;
import com.emmy.walnut.view.iface.SingleClassDetailIface;

import java.util.List;


/**
 *
 */
public class SingleClassDetailPresenter implements SingleClassDetailPIface {

    private SingleClassDetailIface singleClassDetailIface;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==AppConstant.GET_PEROID_RECORD){
                singleClassDetailIface.updateData((List<Record>) msg.obj);
            }
            return false;
        }
    });

    public SingleClassDetailPresenter(SingleClassDetailIface singleClassDetailIface){
        this.singleClassDetailIface = singleClassDetailIface;
    }

    @Override
    public void queryRecordWithPeriod(final String labelName,
                                      final long startTimestamp, final long endTimestamp) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<Record> records =
                        AppDatabase.getInstance().recordDao().getRecordWithPeriodAndLabel(
                                AppConstant.username, labelName, startTimestamp, endTimestamp);
                Message.obtain(handler, AppConstant.GET_PEROID_RECORD, records).sendToTarget();
            }
        }).start();
    }
}
